#include "game.h"

/* 初始化游戏数据 */
GAME init(const int play_count, const int shuffle_count, const int player_count, char *args[])
{
    int game_index = 1;
    //PLAYER players[player_count];
    PLAYER *players;
    for (int i = 0; i < player_count; i++)
    {
        PLAYER player = {i + 1, args[i]};
        players[i] = player;
    }

    GAME game = {game_index, player_count, shuffle_count, 52, players, POKER_CARDS};
    return game;
}
/* 洗牌 */
void shuffle(GAME game)
{
    void swap_poker_card(POKER_CARD *, POKER_CARD *);
    int i = 0;
    int r = 0;

    do
    {
        r = random(game.card_count);
        if (i != r)
            swap_poker_card(&game.poker_cards[i], &game.poker_cards[r]);
    } while (i++ < game.card_count);
}
/* 发牌 */
void deal(GAME game)
{
    int i = 0, j = 0, k = 0;
    //POKER_CARD x, y;

    for (size_t i = 0; i < game.player_count; i++)
    {
        game.players[i].poker_cards = (POKER_CARD *)malloc(sizeof(POKER_CARD) * game.players->card_count);
    }

    do
    {
        do
        {
            game.players[j].poker_cards[i] = game.poker_cards[k];
        } while (j++ < game.player_count);

        k++;
    } while (i++ < game.players->card_count);
}
/* 展示并比较结果 */
void show(GAME game)
{
}
/* 玩一局游戏 */
void play(GAME game)
{
}
/* 回收扑克 */
void recycle(GAME game)
{
}
/* 游戏结束 */
void over(GAME game)
{
}

int compare_player(const void *x, const void *y)
{
    return ((PLAYER *)y)->id - ((PLAYER *)x)->id;
}

void swap_poker_card(POKER_CARD *x, POKER_CARD *y)
{
    POKER_CARD t = *x;
    *x = *y;
    *y = t;
}